<template>
  <div class="dashboard">
    <!-- 数据概览卡片 -->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="data-card" shadow="hover">
          <div class="data-header">
            <div class="title">问卷总数</div>
            <el-icon class="icon" :size="40"><Document /></el-icon>
          </div>
          <div class="data-body">
            <div class="number">{{ stats.questionnaireCount }}</div>
            <div class="desc">个问卷</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="data-card" shadow="hover">
          <div class="data-header">
            <div class="title">用户总数</div>
            <el-icon class="icon" :size="40"><User /></el-icon>
          </div>
          <div class="data-body">
            <div class="number">{{ stats.userCount }}</div>
            <div class="desc">名用户</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="data-card" shadow="hover">
          <div class="data-header">
            <div class="title">今日填写</div>
            <el-icon class="icon" :size="40"><Edit /></el-icon>
          </div>
          <div class="data-body">
            <div class="number">{{ stats.todaySubmissionCount }}</div>
            <div class="desc">份问卷</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="data-card" shadow="hover">
          <div class="data-header">
            <div class="title">活跃问卷</div>
            <el-icon class="icon" :size="40"><DataLine /></el-icon>
          </div>
          <div class="data-body">
            <div class="number">{{ stats.activeQuestionnaireCount }}</div>
            <div class="desc">个进行中</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最新问卷列表 -->
    <el-card class="latest-questionnaires" shadow="hover">
      <template #header>
        <div class="card-header">
          <span>最新问卷</span>
          <el-button type="primary" link @click="$router.push('/admin/questionnaire')">
            查看更多
          </el-button>
        </div>
      </template>
      <el-table :data="stats.latestQuestionnaires" style="width: 100%">
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="description" label="描述" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '进行中' : '未开始' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getStats } from '@/api/dashboard'
import { Document, User, Edit, DataLine } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const stats = ref({
  questionnaireCount: 0,
  userCount: 0,
  todaySubmissionCount: 0,
  activeQuestionnaireCount: 0,
  latestQuestionnaires: []
})

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  return new Date(dateStr).toLocaleString()
}

const loadStats = async () => {
  try {
    const res = await getStats()
    if (res.code === 200) {
      stats.value = res.data
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
    ElMessage.error('获取统计数据失败')
  }
}

onMounted(() => {
  loadStats()
})
</script>

<style lang="scss" scoped>
.dashboard {
  padding: 20px;

  .data-card {
    height: 180px;
    background: linear-gradient(to right bottom, #fff, #f8f9fa);
    transition: all 0.3s;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
    }

    .data-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;

      .title {
        font-size: 16px;
        color: #606266;
      }

      .icon {
        color: #409EFF;
        opacity: 0.8;
      }
    }

    .data-body {
      text-align: center;

      .number {
        font-size: 36px;
        font-weight: bold;
        color: #303133;
        margin-bottom: 8px;
      }

      .desc {
        font-size: 14px;
        color: #909399;
      }
    }
  }

  .latest-questionnaires {
    margin-top: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      span {
        font-size: 16px;
        font-weight: 500;
        color: #303133;
      }
    }

    :deep(.el-table) {
      .el-table__header {
        background-color: #f5f7fa;
      }

      .el-table__row {
        transition: all 0.3s;

        &:hover {
          background-color: #f5f7fa;
        }
      }
    }
  }
}
</style> 